package com.strange.mdl.module.mdl.controller.admin.sutras;

import com.strange.mdl.framework.apilog.core.annotation.ApiAccessLog;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import com.strange.mdl.framework.excel.core.util.ExcelUtils;

import com.strange.mdl.module.mdl.controller.admin.sutras.vo.chapters.MdlSutraChaptersPageReqVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.chapters.MdlSutraChaptersRespVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.chapters.MdlSutraChaptersSaveReqVO;
import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutraChaptersDO;
import com.strange.mdl.module.mdl.service.sutras.MdlSutraChaptersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.strange.mdl.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 经书章节")
@RestController
@RequestMapping("/mdl/sutra-chapters")
@Validated
public class MdlSutraChaptersController {

    @Resource
    private MdlSutraChaptersService sutraChaptersService;

    @PostMapping("/create")
    @Operation(summary = "创建经书章节")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-chapters:create')")
    public CommonResult<Long> createSutraChapters(@Valid @RequestBody MdlSutraChaptersSaveReqVO createReqVO) {
        return success(sutraChaptersService.createSutraChapters(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新经书章节")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-chapters:update')")
    public CommonResult<Boolean> updateSutraChapters(@Valid @RequestBody MdlSutraChaptersSaveReqVO updateReqVO) {
        sutraChaptersService.updateSutraChapters(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除经书章节")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdl:sutra-chapters:delete')")
    public CommonResult<Boolean> deleteSutraChapters(@RequestParam("id") Long id) {
        sutraChaptersService.deleteSutraChapters(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除经书章节")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-chapters:delete')")
    public CommonResult<Boolean> deleteSutraChaptersList(@RequestParam("ids") List<Long> ids) {
        sutraChaptersService.deleteSutraChaptersListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得经书章节")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-chapters:query')")
    public CommonResult<MdlSutraChaptersRespVO> getSutraChapters(@RequestParam("id") Long id) {
        MdlSutraChaptersDO sutraChapters = sutraChaptersService.getSutraChapters(id);
        return success(BeanUtils.toBean(sutraChapters, MdlSutraChaptersRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得经书章节分页")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-chapters:query')")
    public CommonResult<PageResult<MdlSutraChaptersRespVO>> getSutraChaptersPage(@Valid MdlSutraChaptersPageReqVO pageReqVO) {
        PageResult<MdlSutraChaptersDO> pageResult = sutraChaptersService.getSutraChaptersPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MdlSutraChaptersRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出经书章节 Excel")
    @PreAuthorize("@ss.hasPermission('mdl:sutra-chapters:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSutraChaptersExcel(@Valid MdlSutraChaptersPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MdlSutraChaptersDO> list = sutraChaptersService.getSutraChaptersPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "经书章节.xls", "数据", MdlSutraChaptersRespVO.class,
                BeanUtils.toBean(list, MdlSutraChaptersRespVO.class));
    }

}
